from typing import List


"""
https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/
"""
class Solution:
    def findUnsortedSubarray(self, nums: List[int]) -> int:
        l = 0
        r = len(nums) - 1
        snums = sorted(nums)

        while l<=r and nums[l] == snums[l]: l += 1
        while l<=r and nums[r] == snums[r]: r -= 1

        return max(0, r-l+1)
